//
//  jxsTableViewCellController.m
//  ToDoList
//
// This code file is apart of project "iPhone-ToDo-List" 
// (http://code.google.com/p/iphone-todo-list/)
//
// Copyright (c) 2009 Frank Villasenor
//
// Distributed under the terms of the Lesser GNU General Public License.
// A copy of the license should be with these files. If not, you 
// can retrieve a copy from http://www.gnu.org/licenses/lgpl-3.0.txt
// 

#import "jxsTableViewCellController.h"
#import "NSDate(DateFormatting).h"
#import "tosuTask.h"

@implementation jxsTableViewCellController

@synthesize cell;
@synthesize task;
@synthesize titleLabel;
@synthesize dueDateLabel, dueDateLabelLabel;
@synthesize completeNotice;

- (void)dealloc 
{
	[self removeObserver:self forKeyPath:@"task"];
	[cell release];
	[task release];
	[titleLabel release];
	[dueDateLabel release];
	[dueDateLabelLabel release];
	[completeNotice release];
	
	[super dealloc];
}

- (id)init
{
	//
	// Derived from: http://bill.dudney.net/roller/objc/entry/uitableview_from_a_nib_file
	self = [super init];
	if( self != nil )
	{
		[self addObserver:self forKeyPath:@"task" options:NSKeyValueObservingOptionNew context:NULL];
	}
	return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath                         
	ofObject:(id)object                           
	change:(NSDictionary *)change                         
	context:(void *)context  //For an unknown reason, the void needs to be a pointer...
{
	if( [keyPath isEqualToString:@"task"] )
	{
		titleLabel.text = task.title;
		dueDateLabel.text = [task.dueDate getShortDateWithoutTime]; 
		completeNotice.hidden = !task.isComplete;
		
		if( task.isComplete )
		{
			titleLabel.textColor = [UIColor grayColor];
			dueDateLabel.textColor = [UIColor grayColor];
			dueDateLabelLabel.textColor = [UIColor grayColor];
		}
		else 
		{
			titleLabel.textColor = [UIColor blackColor];
			dueDateLabel.textColor = [UIColor blackColor];
			dueDateLabelLabel.textColor = [UIColor blackColor];
		}

	}
}

@end
